package com.hy.hywebview

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.hy.hywebview.databinding.ActivityHyWebviewBinding

/**
 * @Description:
 * @Author: wdk
 * @CreateTime: 2024/4/8 22:33
 */
class HYWebViewActivity : AppCompatActivity() {

    private lateinit var mBinding: ActivityHyWebviewBinding
    private var mUrl = ""
    private var mTitle = "WebView"
    private var mCanNativeRefresh: Boolean = true
    private var mIsShowActionBar: Boolean = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityHyWebviewBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        initParams()
        initView()
    }

    private fun initParams() {
        intent?.extras?.apply {
            mUrl = getString(HYActivityConfig.HY_WEBVIEW_ACTIVITY_URL) ?: "https://www.baidu.com"
            mTitle = getString(HYActivityConfig.HY_WEBVIEW_ACTIVITY_TITLE) ?: "WebView"
            mIsShowActionBar = getBoolean(HYActivityConfig.HY_WEBVIEW_ACTIVITY_IS_SHOW_ACTIONBAR)
            mCanNativeRefresh = getBoolean(HYFragmentConfig.HY_WEBVIEW_FRAGMENT_CAN_NATIVE_REFRESH)
        }
    }

    private fun initView() {
        mBinding.title.text = mTitle
        if (!mIsShowActionBar) {
            mBinding.rlActionBar.visibility = View.GONE
        }
        mBinding.back.setOnClickListener { finish() }

        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        val fragment: Fragment =
            HYWebViewFragment.newInstance(url = mUrl, canNativeRefresh = mCanNativeRefresh)
        transaction.replace(R.id.web_view_fragment, fragment).commit()
    }

    fun updateTitle(title: String) {
        mBinding.title.text = title
    }
}